//
//  ImagePickerViewController.swift
//  DemoProject
//
//  Created by hao on 2018/7/18.
//  Copyright © 2018年 hao. All rights reserved.
//

import UIKit

class ImagePickerViewController: BaseTableViewController {

    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupData()
        let isUseCamera = UIImagePickerController.isSourceTypeAvailable(.camera)
        print(info: "是否可以使用照相机: \(isUseCamera)")
        Observable<Int>.timer(1, scheduler: MainScheduler.instance).subscribe {
            // 判断照片源是否可以使用
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                print(info: "源可用")
                let imagePicker = UIImagePickerController()
                imagePicker.sourceType = .photoLibrary
                // 设置允许修改
                imagePicker.allowsEditing = true
                imagePicker.delegate = self
                self.present(imagePicker, animated: true, completion: nil)
            } else {
                print(info: "源不可用")
            }
        }.disposed(by: disposeBag)
    }
    
    private func setupData() {
        let titles = ["检查指定源是否在设备上可用",
                      "allowsEditing(默认NO)",
                      "videoMaximumDuration",
                      "availableMediaTypesForSourceType",
                      "sourceType",
                      "代理回调参数中字典info的key"]
        
        let descArr = ["+(BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;",
                       "是否允许编辑 [imagePicker setAllowsEditing:YES] 允许编辑.",
                       "设置UIImagePicker的最大视频持续时间.默认10分钟",
                       """
                        指定源可用的媒体种类
                        //获得相机模式下支持的媒体类型
                        NSArray* availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
                       """,
                       """
                        设置UIImagePicker照片源类型，默认有3种。
                        照片源类型
                        UIImagePickerControllerSourceTypeCamera            照相机
                        UIImagePickerControllerSourceTypePhotoLibrary      照片库（通过同步存放的，用户不能删除）
                        UIImagePickerControllerSourceTypeSavedPhotosAlbum  保存的照片（通过拍照或者截屏保存的，用户可以删除）
                       """,
                       """
                        NSString *const UIImagePickerControllerMediaType;// 媒体类型
                        NSString *const UIImagePickerControllerOriginalImage;// 原始未编辑的图像
                        NSString *const UIImagePickerControllerEditedImage;// 编辑后的图像
                        NSString *const UIImagePickerControllerCropRect;// 源图像可编辑(有效?)区域
                        NSString *const UIImagePickerControllerMediaURL;// 视频的路径
                        NSString *const UIImagePickerControllerReferenceURL;// 原始选择项的URL
                        NSString *const UIImagePickerControllerMediaMetadata;// 只有在使用摄像头并且是图像类型的时候有效.包含选择图像信息的字典类型
                       """]
        
        addTitles(titles, footDescs: descArr)
    }

}


extension ImagePickerViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        print(info: "Cancel")
        picker.dismiss(animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        print(info: "info: \(info)")
        picker.dismiss(animated: true, completion: nil)
    }
    
}

